package hlhj.fhp.newslib.activitys

import android.support.v7.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.Response
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.javabean.SpecialBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CustomAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.utils.GlideUtil
import kotlinx.android.synthetic.main.aty_special.*
import kotlinx.android.synthetic.main.hlhj_news_head_lo.*
import kotlinx.android.synthetic.main.hlhj_news_list_lo.*

/**
 * Created by Administrator on 2018\6\25 0025.
 */
class SpecialAty :BaseAty(), SpringView.OnFreshListener {


    private lateinit var adp : BaseQuickAdapter<SpecialBean.DataBean, BaseViewHolder>
    private val datas=ArrayList<SpecialBean.DataBean>()
    private var page=1
    override fun initListener() {
    }

    override fun initView() {
        tvTittle.text="专题"

        adp=object :BaseQuickAdapter<SpecialBean.DataBean,BaseViewHolder>(R.layout.aty_special_listitem,datas){
            override fun convert(helper: BaseViewHolder?, item: SpecialBean.DataBean?) {
                Glide.with(mContext).load(item?.cover_img).apply(GlideUtil.loadIMGOption())
                        .into(helper?.getView(R.id.itemImg)!!)
                helper?.setText(R.id.itemTittle,item?.subject_name)
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager= LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        /*上下拉框架使能*/

        spView.header= DefaultHeader(this)
        spView.setListener(this)
        dispatchData()
    }

    private fun dispatchData() {
        Apis.getSpecialList()
                .doOnSubscribe(LoadingAction(this))
                .subscribe(object :CustomAction<Response<SpecialBean>>{
                    override fun onResult(t: Response<SpecialBean>) {
                        val specialBean = t.body()
                        datas.addAll(specialBean.data)
                        adp.notifyDataSetChanged()
                    }
                })
    }
    override fun onLoadmore() {
    }

    override fun onRefresh() {
        datas.clear()
        dispatchData()
    }
    override fun getContentId(): Int {
        return R.layout.aty_special
    }
}